package template.smarty4j.expression;

import static org.objectweb.asm.Opcodes.*;

import java.util.Map;

import template.smarty4j.MethodVisitorProxy;
import template.smarty4j.VariableManager;

public class MapExpression extends ObjectExpression {

	private Map<String, Expression> value;
	
	public MapExpression(Map<String, Expression> value) {
		this.value = value;
	}

	public Map<String, Expression> getValue() {
		return value;
	}
	
	@Override
	public void parseSelf(MethodVisitorProxy mw, int local, VariableManager vm) {
		mw.visitTypeInsn(NEW, "java/util/HashMap");
		mw.visitInsn(DUP);
		mw.visitMethodInsn(INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
		for (Map.Entry<String, Expression> entry : value.entrySet()) {
			mw.visitInsn(DUP);
			mw.visitLdcInsn(entry.getKey());
			entry.getValue().parseObject(mw, local, vm);
			mw.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
			mw.visitInsn(POP);
		}
	}
}
